package org.cnbi.web.controller.manage;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.cnbi.entity.Menu;
import org.cnbi.entity.News;
import org.cnbi.entity.User;
import org.cnbi.service.IHandleService;
import org.cnbi.service.INewsService;
import org.cnbi.service.ISystemService;
import org.cnbi.utils.date.DateUtil;
import org.cnbi.utils.pagination.PagingUtil;
import org.cnbi.utils.tools.ActionConstants;
import org.cnbi.utils.tools.Constants;
import org.cnbi.utils.tools.GlobalConstants;
import org.cnbi.web.controller.base.BaseController;
import org.cnbi.web.utils.ContextHolderUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/newsM")
public class NewsController extends BaseController {
	protected final Log logger = LogFactory.getLog(getClass());
	
	@Autowired
	private IHandleService handleService;
	
	@Autowired
	public INewsService newsService;
	
	@Autowired
	private ISystemService systemService;
	
	//进入新闻管理页面，默认查询所有的新闻条目
	@RequestMapping(value = "/index")
	public ModelAndView levelTwo(News news,Menu menu,PagingUtil page,HttpServletResponse response) {
	    ModelAndView view = secondNavHandle(menu, response);
	    view.addObject("pageBean", menu);
		
		view.addObject(Constants.DATAS, queryAllNews(page,news));
		return view;
    }
	
	//按时间或者标题查询新闻条目
	/**
	 * @param news
	 * @param menu
	 * @param page
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/query")
	public ModelAndView queryNews(News news,Menu menu,PagingUtil page,HttpServletResponse response) {
		ModelAndView view = secondNavHandle(menu, response);
		String sql = "";
		String title = news.getTitle();
		String updateTime = news.getUpdateTime();
		if(title != null && title.length() != 0){
			sql = getSqlByKey(Constants.NEWS+"title");
			sql = sql.replaceAll(":title", " '%"+title+"%' ");
		}else if(updateTime != null && updateTime.length() != 0){
			sql = getSqlByKey(Constants.NEWS+"updateTime");
			sql = sql.replaceAll(":updateTime", " '%"+updateTime+"%' ");
		}else{
			sql = getSqlByKey(Constants.NEWS+Constants.ALLNEWS);
		}
		
		/*if(news.getUpdateTime().equals("")){
			news.setUpdateTime("");
		}else{
			news.setUpdateTime(" and datediff(day,updateTime,'"+news.getUpdateTime()+"')=0 ");
		}
		sql = sql.replaceAll(":title", news.getTitle()).replaceAll(":updateTime", news.getUpdateTime());*/
		page.setSql(sql);
		view.addObject(Constants.DATAS, queryAllNews(page,news));
		systemService.addLog(getSqlByKey(ActionConstants.MANAGE + ActionConstants.ADD+"Log"),getSysLog("新闻【" + news.getTitle()+ "】查询成功！", GlobalConstants.Log_Type_OTHER, GlobalConstants.Log_Leavel_INFO));
		return view;
	}
	
	//添加新闻功能模块
	@RequestMapping(value = "/add")
	public ModelAndView addNews(News news,Menu menu,PagingUtil page,HttpServletResponse response){
		ModelAndView view = secondNavHandle(menu, response);
		String sql = getSqlByKey(Constants.NEWS+Constants.ADDNEWS);
		//当前登录用户
		User user = (User) ContextHolderUtils.getSession().getAttribute(GlobalConstants.LOGIN_USER);
		news.setCreateUser(user.getUsername());
		news.setUpdateUser(user.getUsername());
		String curentTime = DateUtil.formatDate(new java.util.Date(), "yyyy-MM-dd hh:mm:ss");
		news.setUpdateTime(curentTime);
		news.setCreateTime(curentTime);
		newsService.executeNews(sql, news);	
		systemService.addLog(getSqlByKey(ActionConstants.MANAGE + ActionConstants.ADD+"Log"),getSysLog("新闻【" + news.getTitle()+ "】添加成功！", GlobalConstants.Log_Type_INSERT, GlobalConstants.Log_Leavel_INFO));
		view.addObject(Constants.DATAS, queryAllNews(page,news));
		return view;
	}
	
	//删除新闻功能模块，可以进行单条或多条删除
	@RequestMapping(value = "/del")
	public ModelAndView deleteNews(News news,Menu menu,PagingUtil page,HttpServletRequest request,HttpServletResponse response){
		ModelAndView view = secondNavHandle(menu, response);
		String sql = getSqlByKey(Constants.NEWS+Constants.DELNEWS);
		sql = sql.replaceAll(":id", request.getParameter("ids"));
		
		newsService.executeNews(sql, news);
		systemService.addLog(getSqlByKey(ActionConstants.MANAGE + ActionConstants.ADD+"Log"),getSysLog("新闻【" + news.getTitle()+ "】删除成功！", GlobalConstants.Log_Type_DEL, GlobalConstants.Log_Leavel_INFO));
		view.addObject(Constants.DATAS, queryAllNews(page,news));
		return view;
	}
	
	//编辑选中的新闻功能模块
	@RequestMapping(value = "/edit")
	public ModelAndView deditNews(News news,Menu menu,PagingUtil page,HttpServletResponse response){
		ModelAndView view = secondNavHandle(menu, response);
		String sql = getSqlByKey(Constants.NEWS+Constants.EDITNEWS);		
		news.setUpdateTime(DateUtil.formatDate(new java.util.Date(), "yyyy-MM-dd hh:mm:ss"));
		User user = (User) ContextHolderUtils.getSession().getAttribute(GlobalConstants.LOGIN_USER);
		news.setUpdateUser(user.getUsername());
		newsService.executeNews(sql, news);
		systemService.addLog(getSqlByKey(ActionConstants.MANAGE + ActionConstants.ADD+"Log"),getSysLog("新闻【" + news.getTitle()+ "】编辑成功！", GlobalConstants.Log_Type_UPDATE, GlobalConstants.Log_Leavel_INFO));
		view.addObject(Constants.DATAS, queryAllNews(page,news));
		return view;
	}
	
	//查询所有的新闻条目方法
	public Object queryAllNews(PagingUtil page,News news){
		if(null==page.getSql()){
			String sql = getSqlByKey(Constants.NEWS+Constants.ALLNEWS);
			page.setSql(sql);
		}
		return newsService.queryAllNews(page, news);
	}
}
